package zelix.gui.clickguis.kendall.frame;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Mouse;
import scala.Float;
import zelix.Core;
import zelix.gui.clickguis.N3ro.Utils.RenderUtil;
import zelix.gui.clickguis.kendall.components.impls.KendallButton;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.hack.hacks.ClickGui;
import zelix.utils.hooks.visual.font.FontLoaders;
import zelix.utils.tenacityutils.render.blur.BloomUtil;
import zelix.utils.tenacityutils.render.blur.GaussianBlur;

import java.util.ArrayList;

public class KendallFrame {

    public float x = 0;
    public float y = 0;
    public HackCategory category;
    public boolean onMoving = false;

    public boolean ListOpen=true;
    private ArrayList<KendallButton> buttons = new ArrayList<KendallButton>();

    public KendallFrame(HackCategory category, int x, int y){
        this.category = category;
        this.x = x;
        this.y = y;
    }
    public void initialize() {
        float y2 = y+20;
        //width 100 5 90 5
        this.buttons.clear();

        for (Hack hack: Core.hackManager.getModulesInType(category)) {
            this.buttons.add(new KendallButton(hack,x,y2,this));
            y2 += 18;
        }

        Minecraft mc=Minecraft.getMinecraft();
        if (OpenGlHelper.shadersSupported) {
            if (mc.entityRenderer.getShaderGroup() != null) {
                mc.entityRenderer.getShaderGroup().deleteShaderGroup();
            }
            mc.entityRenderer.loadShader(new ResourceLocation("shaders/post/blur.json"));

        }
    }
    public float moveX = 0, moveY = 0;

    public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
        if(ListOpen){
            for (KendallButton bt: this.buttons) {
                bt.onClick(mouseX,mouseY,mouseButton);
            }
        }


        if (isHovered(this.x,this.y,this.x +100,this.y+20, mouseX, mouseY) && mouseButton ==0){
            for (KendallFrame frame : ClickGui.KendallMyGod.frames){
                if (frame.onMoving){
                    System.out.println(frame.category.name() + " MOVING");
                    return;
                }
            }
            onMoving = true;
        }
        if(isHovered(this.x,this.y,this.x +100,this.y+20, mouseX, mouseY) && mouseButton ==1){
            ListOpen=!ListOpen;
        }

    }

    public void mouseReleased(int mouseX, int mouseY, int state) {
        onMoving = false;
    }

    public void keyTyped(char typedChar, int keyCode) {

    }

    public void render(int mouseX, int mouseY) {

        if (isHovered(this.x,this.y,this.x +100,this.y+20, mouseX, mouseY) && Mouse.isButtonDown(0) && onMoving) {

            if (moveX == 0 && moveY == 0) {
                moveX = mouseX -  this.x;
                moveY = mouseY - this.y;
            } else {
                this.x = mouseX - moveX;
                this.y = mouseY - moveY;

                float y2 = y+20;
                for (KendallButton bt: this.buttons) {
                    bt.y = y2;
                    y2 += 18;
                }

            }
            //this.previousmouse = true;
        } else if (moveX != 0 || moveY != 0) {
            moveX = 0;
            moveY = 0;

        }

        if(x<0 ){
            x = 0;

        } else if (y < 0) {
            y = 0;
        }

        KendallButton.drawColor=0xFFFFFFFF;

        if(!ListOpen){
            zelix.utils.tenacityutils.render.RenderUtil.renderRoundedRect((float)this.x,(float)this.y,(float)100,(float)20, 2,0xFF161616 );
        }else{
            RenderUtil.drawRoundRect_up((double)this.x,(double)this.y,(double)this.x +100,(double)this.y+20, 1,0xFF161616 );
        }

        String OpenIcon = this.ListOpen ? "▼" : "▶";
        Minecraft.getMinecraft().fontRenderer.drawString(OpenIcon,(int)(x+100-10),(int)(y+12.5/2f),0xFFF5F5F7);

        //RenderUtil.drawBorderedRect(this.x,this.y,this.x +100,this.y+20, 1,0xFF161616 ,0xFF161616);
        //BLur.draw(this.x,this.y,100,20,0,10f);

        FontLoaders.default14.drawStringWithShadow(this.category.name(), this.x +8,this.y+8, KendallButton.drawColor);

        if(ListOpen){

            for (KendallButton bt: this.buttons) {
                if(bt.equals(this.buttons.get(this.buttons.size() - 1))){
                    bt.onRender(mouseX,mouseY,true);
                }else{
                    bt.onRender(mouseX,mouseY,false);
                }
            }
        }

    }


    public static boolean isHovered(float x, float y, float x2, float y2, int mouseX, int mouseY) {
        if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2) {
            return true;
        }

        return false;
    }

}
